package com.nyp.remoter.web;

import java.io.Serializable;

import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.event.AjaxBehaviorEvent;
import javax.inject.Named;

import com.nyp.remoter.ejb.EntityIsolationEJB;
import com.nyp.remoter.entity.User;

@Named
@SessionScoped
public class IsolationJSFBean implements Serializable {

	private static final long serialVersionUID = 9171051448078187894L;

	@EJB
	EntityIsolationEJB isolationEJB;
	
	private Integer secondsToWait;
	private Long userId;
	private String newName;
	private String selectStatusMessage;
	private String updateStatusMessage;
	
	public String startSelect(AjaxBehaviorEvent e){
		try{
			User u = isolationEJB.findUserByIDAndWait(userId, secondsToWait);
			if(u == null){
				selectStatusMessage = "There is no user with this id.";
			}
			else{
				selectStatusMessage = "User: " + u.getName() + " found succesfully";
			}
		}
		catch(Exception ex){
			selectStatusMessage = "Exception during isolationEJB.findUserByIDAndWait. See the logs";
			ex.printStackTrace();
		}
		
		return null;
	}
	
	public String startUpdate(AjaxBehaviorEvent e){
		try{
			isolationEJB.changeUserName(userId, newName);
			updateStatusMessage = "User updated succesfully";
		}
		catch(Exception ex){
			updateStatusMessage = "Exception during isolationEJB.changeUserName(). See the logs";
			ex.printStackTrace();
		}
		return null;
	}
	
	public Integer getSecondsToWait() {
		return secondsToWait;
	}
	public void setSecondsToWait(Integer secondsToWait) {
		this.secondsToWait = secondsToWait;
	}
	public Long getUserId() {
		return userId;
	}
	public void setUserId(Long userId) {
		this.userId = userId;
	}
	public String getNewName() {
		return newName;
	}
	public void setNewName(String newName) {
		this.newName = newName;
	}

	public String getSelectStatusMessage() {
		return selectStatusMessage;
	}

	public void setSelectStatusMessage(String selectStatusMessage) {
		this.selectStatusMessage = selectStatusMessage;
	}

	public String getUpdateStatusMessage() {
		return updateStatusMessage;
	}

	public void setUpdateStatusMessage(String updateStatusMessage) {
		this.updateStatusMessage = updateStatusMessage;
	}

}
